package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.config.ServerConfig;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardServeLibraryNews;
import com.xydtech.ecard.config.service.IEcardServeLibraryNewsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 电子名片文库模块文库信息Controller
 *
 * @author yuzhiguo
 * @date 2024-09-18 下午2:11
 */
@Controller
@RequestMapping("/ecard/config/serveLibraryNews")
public class EcardServeLibraryNewsController extends BaseController {

    private String prefix = "ecard/config/serveLibraryNews";

    @Autowired
    private IEcardServeLibraryNewsService ecardServeLibraryNewsService;

    @Autowired
    private ServerConfig serverConfig;

    @RequiresPermissions("ecard:config:serveLibraryNews:view")
    @GetMapping("/{libId}")
    public String serveLibraryNews(@PathVariable("libId") Long libId, ModelMap mmap) {
        mmap.put("libId", libId);
        return prefix + "/serveLibraryNews";
    }

    /**
     * 查询电子名片文库模块文库信息列表
     */
    @RequiresPermissions("ecard:config:serveLibraryNews:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardServeLibraryNews ecardServeLibraryNews)
    {
        startPage();
        List<EcardServeLibraryNews> list = ecardServeLibraryNewsService.selectEcardServeLibraryNewsList(ecardServeLibraryNews);
        return getDataTable(list);
    }

    /**
     * 导出电子名片文库模块文库信息列表
     */
    @RequiresPermissions("ecard:config:serveLibraryNews:export")
    @Log(title = "电子名片文库模块文库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardServeLibraryNews ecardServeLibraryNews)
    {
        List<EcardServeLibraryNews> list = ecardServeLibraryNewsService.selectEcardServeLibraryNewsList(ecardServeLibraryNews);
        ExcelUtil<EcardServeLibraryNews> util = new ExcelUtil<EcardServeLibraryNews>(EcardServeLibraryNews.class);
        return util.exportExcel(list, "电子名片文库模块文库信息数据");
    }

    /**
     * 新增电子名片文库模块文库信息
     */
    @GetMapping("/add/{libId}")
    public String add(@PathVariable("libId") Long libId, ModelMap mmap) {
        mmap.put("libId", libId);
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片文库模块文库信息
     */
    @RequiresPermissions("ecard:config:serveLibraryNews:add")
    @Log(title = "电子名片文库模块文库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardServeLibraryNews ecardServeLibraryNews, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardServeLibraryNews.setLibContent(serverConfig.replaceContextPath(request, ecardServeLibraryNews.getLibContent()));
        return toAjax(ecardServeLibraryNewsService.insertEcardServeLibraryNews(ecardServeLibraryNews));
    }

    /**
     * 修改电子名片文库模块文库信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, HttpServletRequest request, ModelMap mmap) {
        EcardServeLibraryNews ecardServeLibraryNews = ecardServeLibraryNewsService.selectEcardServeLibraryNewsById(id);
        //富文本summerNote内容图片显示，ContextPath替换处理（ContextPath配置后缀使用）
        String contextPath = request.getServletContext().getContextPath();
        if (contextPath.length() > 1 && ecardServeLibraryNews.getLibContent() != null) {
            ecardServeLibraryNews.setLibContent(ecardServeLibraryNews.getLibContent().replaceAll("/profile", contextPath + "/profile"));
        }
        mmap.put("ecardServeLibraryNews", ecardServeLibraryNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片文库模块文库信息
     */
    @RequiresPermissions("ecard:config:serveLibraryNews:edit")
    @Log(title = "电子名片文库模块文库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardServeLibraryNews ecardServeLibraryNews)
    {
        return toAjax(ecardServeLibraryNewsService.updateEcardServeLibraryNews(ecardServeLibraryNews));
    }

    /**
     * 删除电子名片文库模块文库信息
     */
    @RequiresPermissions("ecard:config:serveLibraryNews:remove")
    @Log(title = "电子名片文库模块文库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardServeLibraryNewsService.deleteEcardServeLibraryNewsByIds(ids));
    }
}
